<template>
    <div class="login">
        <div class="loginform">
            <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px"
                class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username" type="text" autocomplete="off" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
                </el-form-item>
                <el-form-item label="验证码" prop="captcha">
                    <el-space>
                        <el-input v-model="ruleForm.captcha" />
                        <div @click="changeimg">
                            <img :src="captchaimg" alt="">
                        </div>
                    </el-space>

                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                    <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

    </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'

import {getcaptcha} from '../api/captcha'
import {loginapi} from '../api/login'

const router = useRouter()
const captchaimg = ref('')
const key = ref('')

getcaptcha().then(res => {
    console.log(res)
    captchaimg.value = res.data.img
    key.value = res.data.key
})

const changeimg = () =>{
    getcaptcha().then(res => {
    console.log(res)
    captchaimg.value = res.data.img
    key.value = res.data.key
})
}

const ruleFormRef = ref()

const ruleForm = reactive({
    username: '',
    password: '',
    captcha: '',
    key: ''
})

const rules = reactive({
    username: [{ required:true, message:'请输入用户名', trigger: 'blur' }],
    password: [{ required:true, message:'请输入密码', trigger: 'blur' }],
    captcha: [{ required:true, message:'请输入验证码', trigger: 'blur' }],
})

const submitForm = (formEl) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            ruleForm.key = key.value
            // console.log(ruleForm)
            loginapi(ruleForm).then(res => {
                console.log('1111',res)
                if(res.data.errNo == 0){
                    localStorage.setItem('jwt',res.data.context.jwt)
                    localStorage.setItem('acl',JSON.stringify(res.data.context.acl))

                    
                    router.push('/dashboard')
                }
            })

        } else {
            console.log('error submit!')
            return false
        }
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>

<style lang="scss" scoped>
.login {
    display: flex;
    width: 100%;
    height: 100%;
    justify-content: center;
    align-items: center;

    .loginform {
        width: 500px;
    }
}
</style>